package com.zzzyh.forumsystemclient.cotroller;

import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.model.Message;
import com.zzzyh.forumsystemclient.services.IMessageService;
import com.zzzyh.forumsystemclient.utils.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/message")
@Slf4j
public class MessageController {
    @Autowired
    private IMessageService messageService;
    @RequestMapping("/list")
    public AppResult<List<Message>> list() {
        return AppResult.success(messageService.list());
    }

    @RequestMapping("/add")
    public AppResult<Boolean> add(@RequestBody Message message, HttpServletRequest request) {

        // 获取客户端 IP 地址
        String ipAddress = IpUtil.getIp(request);
        String source = IpUtil.getCityInfo(ipAddress);
        // 获取浏览器信息
        String browserInfo = IpUtil.getBrowserInfo( request);

        // 设置到消息对象中
        message.setIp(ipAddress);
        message.setBrowser(browserInfo);
        message.setSource(source);
        messageService.add(message);
        return AppResult.success(true);

    }
}
